package com.asia5b.a5mall.order.fragment

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import android.text.TextUtils
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import com.asia5b.a5mall.middleware.base.SimpleDialogFragment
import com.asia5b.a5mall.middleware.base.SimpleViewModel
import com.asia5b.a5mall.middleware.config.UserRepository
import com.asia5b.a5mall.middleware.model.bean.OrderListType
import com.asia5b.a5mall.middleware.utils.Preference
import com.asia5b.a5mall.middleware.utils.judge
import com.asia5b.a5mall.middleware.utils.newDivider
import com.asia5b.a5mall.middleware.utils.sNewStanceFragment
import com.asia5b.a5mall.middleware.widget.BaseTextWatcher
import com.asia5b.a5mall.order.R
import com.asia5b.a5mall.order.adapter.SearchOrderKeyAdapter
import com.asia5b.a5mall.order.databinding.DfSearchorderBinding
import kotlinx.android.synthetic.main.df_searchorder.*

/**
 * Created by zhangyuncai on 2020/3/17.
 */
class SearchOrderDF : SimpleDialogFragment<SimpleViewModel, DfSearchorderBinding>() {
    override val viewModelClazz: Class<SimpleViewModel>
        get() = SimpleViewModel::class.java

    override fun getDataBinding(container: ViewGroup?): DfSearchorderBinding {
        return DfSearchorderBinding.inflate(layoutInflater,container,false)
    }

    lateinit var currentOrderListType: OrderListType

    var historyTexts by Preference("${UserRepository.getUserInfo().member_id}history", "")
    var historyDatas = arrayListOf<String>()
    val historyAdapter by lazy { SearchOrderKeyAdapter(historyDatas) }
    val historyRecyclerView by lazy { rv_searchorder_history }

    fun getHistory() {
        historyDatas.clear()
        if (!TextUtils.isEmpty(historyTexts)) {
            for (s in historyTexts.split(",")) {
                historyDatas.add(s)
            }
        }
        historyAdapter.notifyDataSetChanged()
    }

    fun addHistory(history: String) {
        if (historyDatas.contains(history)) {
            historyDatas.remove(history)
        }
        historyDatas.add(0,history)
        historyTexts = TextUtils.join(",", historyDatas)
        getHistory()
    }


    override val isStatus: Boolean
        get() = true


    override fun initView() {
        historyRecyclerView.run {
            layoutManager = LinearLayoutManager(mActivity)
            adapter = historyAdapter
            addItemDecoration(newDivider(mActivity))
        }
        getHistory()
    }

    override fun initData() {
    }

    fun search(key: String) {
        addHistory(key)
        var bundle = Bundle()
        bundle.putSerializable("type", currentOrderListType)
        bundle.putSerializable("goods_name", key)
        val fragment = sNewStanceFragment(mActivity, OrderListFragment::class.java, bundle)
        childFragmentManager.beginTransaction()
                .replace(R.id.fl_searchorder_vessel, fragment)
                .commit()
    }

    override fun initEvent() {
        tv_searchorder_colse.setOnClickListener {
            val key = et_searchorder_key.text.toString()
            if (TextUtils.isEmpty(key)) {
                dismiss()
            } else {
                search(key)
            }
        }
        et_searchorder_key.addTextChangedListener(object : BaseTextWatcher() {
            override fun onTextChanged(charSequence: CharSequence?, start: Int, count: Int, after: Int) {
                val key = et_searchorder_key.text.toString()
                tv_searchorder_colse.text = (TextUtils.isEmpty(key)).judge(getString(R.string.取消), getString(R.string.搜索))
                iv_searchorder_clear.visibility = (TextUtils.isEmpty(key)).judge(View.GONE, View.VISIBLE)
                if (TextUtils.isEmpty(key)) {
                    nsv_searchorder_history.visibility = View.VISIBLE
                }
            }
        })
        iv_searchorder_clear.setOnClickListener {
            et_searchorder_key.setText("")
        }
        nsv_searchorder_history.setOnClickListener { }
        bt_searchorder_clearhistory.setOnClickListener {
            historyTexts = ""
            getHistory()
        }
        historyAdapter.setOnItemClickListener { adapter, view, position ->
            val key = adapter.getItem(position) as String
            et_searchorder_key.setText(key)
            search(key)
        }
    }

    fun getDatasResult() {
        nsv_searchorder_history.visibility = View.GONE
    }

    override val heightStyle: Int
        get() = WindowManager.LayoutParams.MATCH_PARENT
}